﻿//Validations functions
var regExpCountryCode = /^(\+|\d)[0-9]{1,4}$/;
var regExpMobileNumber = /^(\+|\d)[0-9]{7,16}$/;
var regExEmailValidation = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
var regExPassWordValidation = /^((?=.*\d)(?=.*[A-Z])(?=.*[@#$%]).{8,20})$/;
/*/^[a-zA-Z0-9!@#$%^&*]{6,}$/*/;
var date_regex = /^(0[1-9]|1[0-2])\-(0[1-9]|1\d|2\d|3[01])\-(19|20)\d{2}$/;
var time_regex = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;

var specialKeys = new Array();
specialKeys.push(8); //Backspace
specialKeys.push(9); //Tab
specialKeys.push(46); //Delete
specialKeys.push(36); //Home
specialKeys.push(35); //End
specialKeys.push(37); //Left
specialKeys.push(39); //Right

$(document).ready(function () {
    $(".numericonly").ForceNumericOnly();
    $(".digitonly").ForceDigitOnly();
    $(".nameonly").ForceNameCharacter();
    $(".timeonly").ForceTimeOnly();
    $(".dateonly").ForceDateOnly();
    $(".commaseperatedcharacter").ForceCommaSeperatedNameCharacter();
    $(".message").click(function () {
        $(this).fadeOut();
    });

    $("#globaldiv").click(function () {
        $(this).fadeOut();
    });
});

//@@@ Validate datetime format

function validateDate(Source, args) {
    var testdate = args.Value;
    if (testdate != '') {
        if (date_regex.test(testdate)) {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }
    }
    else {
        args.IsValid = true;

    }
}
//@@@ validate time
function validate_timeformat(time) {
    if (time != '') {
        if (time_regex.test(time)) {
            return true;
        }
        else {
            return false;
        }
    }
}

// validate country code
function Validate_CountryCode(Source, args) {
    var countryCode = args.Value;
    if (countryCode != '') {
        if (regExpCountryCode.test(countryCode)) {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }
    }
    else {
        args.IsValid = false;

    }
}

// validate mobile numbers
function Validate_MobileNumber(Source, args) {
    var mobileNumber = args.Value;
    if (mobileNumber != '') {
        if (regExpMobileNumber.test(mobileNumber)) {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }
    }
    else {
        args.IsValid = false;
    }
}

function Validate_EmailAddress(Source, args) {

    var emailaddress = args.Value;
    if (emailaddress != '') {
        if (regExEmailValidation.test(emailaddress)) {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }
    }
    else {
        args.IsValid = false;
    }
}

// Numeric only control handler
jQuery.fn.ForceNumericOnly =
function () {
    return this.each(function () {
        $(this).keydown(function (e) {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            // home, end, period, and numpad decimal
            return (
                key == 8 ||
                key == 9 ||
                key == 46 ||
                key == 110 ||
                key == 190 ||
                key == 13 ||
                (key >= 35 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        });
    });
};

// Digit only control handler
jQuery.fn.ForceDigitOnly =
function () {
    return this.each(function () {
        $(this).keydown(function (e) {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            // home, end, period, and numpad decimal
            return (
                key == 8 ||
                key == 9 ||
                key == 46 ||
                (key >= 35 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        });
    });
};

function fn_ForceDigitOnly(p_vEvent) {
    var key = p_vEvent.keyCode == 0 ? p_vEvent.charCode : p_vEvent.keyCode;
    var ret = (key == 127 || key == 8 || key == 9 || key == 46 ||
                (key >= 35 && key <= 40) ||
                (key >= 48 && key <= 57));
    return ret;
}

// time only control handler ex.  10:00 
jQuery.fn.ForceTimeOnly =
function () {
    return this.each(function () {
        $(this).keydown(function (e) {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            // home, end, period, and numpad decimal
            return (
                key == 8 ||
                key == 9 ||
                key == 46 ||
                key == 110 ||
                key == 186 ||
                (key >= 35 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        });
    });
};

// time only control handler ex.  10:00 
jQuery.fn.ForceDateOnly =
function () {
    return this.each(function () {
        $(this).keydown(function (e) {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            // home, end, period, and numpad decimal
            return (
                key == 8 ||
                key == 9 ||
                key == 46 ||
                key == 110 ||
                key == 109 ||
                key == 189 ||
                (key >= 35 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        });
    });
};


// Name characters only control handler
jQuery.fn.ForceNameCharacter =
function () {
    return this.each(function () {
        $(this).keydown(function (e) {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            // home, end, period, and numpad decimal
            return (
                key == 8 ||
                key == 9 ||
                key == 46 ||
                key == 32 ||
                key == 110 ||
                key == 190 ||
                key == 222 ||
                (key >= 65 && key <= 90)
               );
        });
    });
};

// Name characters only control handler
jQuery.fn.ForceCommaSeperatedNameCharacter =
function () {
    return this.each(function () {
        $(this).keydown(function (e) {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            // home, end, period, and numpad decimal
            return (
                key == 8 ||
                key == 9 ||
                key == 46 ||
                key == 32 ||
                key == 110 ||
                key == 190 ||
                key == 222 ||
                key == 188 ||
                (key >= 65 && key <= 90)
               );
        });
    });
};

///@@@ Validate email address format
function validate_EmailAddress(p_EmailAddress) {
    if (regExEmailValidation.test(p_EmailAddress)) {
        return true;
    }
    else {
        return false;
    }
}
///@@@ Validate password allow alphanumeric minimum 6 digit password
function Validate_PassWord(Source, args) {
    var emailaddress = args.Value;
    if (emailaddress != '') {
        if (emailaddress.length >= 8) {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }
    }
    else {

        args.IsValid = false;
    }
}

/// <summary>
/// Used to validate a control accepting time in 24 hrs format
/// </summary>

function fn_TimeValidation(Source, args) {

    var strValue = args.Value;
    if (strValue == "") {
        args.IsValid = false;
    }
    else {
        if (strValue.indexOf(':') > -1) {
            var arr = strValue.split(':');
            var arr1 = arr[0];
            var arr2 = arr[1];

            if (arr1 > 0 && arr1 <= 23) {
                if (arr2 >= 0 && arr2 <= 59) {
                    args.IsValid = true;
                }
                else {
                    args.IsValid = false;
                }
            }
            else {
                args.IsValid = false;
            }
        }
        else {
            var arr = strValue;
            if (arr > 0 && arr <= 23) {
                args.IsValid = true;
            }
            else {
                args.IsValid = false;
            }
        }
    }

}

function jsValid_SingleValidate(id) {
    var validator = document.getElementById(id);
    validator.isvalid = true;
    ValidatorUpdateDisplay(validator);
    ValidatorValidate(validator);
    return validator.isvalid;
}


function fn_ValidateNumericVal(p_vVal) {
    if ((isNaN(Trim(p_vVal))) || (parseFloat(p_vVal) < 0) || (Trim(p_vVal) == '')) {
        return false;
    }
    else {
        return true;
    }
}

function ValidateEachEmailID(obj, args) {
    var regEmail = /^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/;
    if (args.Value.indexOf(';') == -1)
        args.IsValid = regEmail.test(args.Value);
    else {
        var emails = args.Value.split(';');
        var blnIsValidEmail = true;
        for (var em = 0; em < emails.length; em++) {
            if (regEmail.test(emails[em]) == false) {
                blnIsValidEmail = false;
                break;
            }
        }
        args.IsValid = blnIsValidEmail;
    }
}